
function Block(idx, user) {
	this.idx = idx;
	this.user = user;
    attrId = 'id="block' + idx + '" ';
    this.code = '<span  class="spBlock pointer" ' + attrId +'>+ </span>';
	switch (user) {
		case 'Yellow'	:	$('#col2').append(this.code);
							$('#block' + idx).addClass("blYellow");
							break;
		case 'Blue'		: 	$('#col3').append(this.code);
							$('#block' + idx).addClass("blBlue");
							break;
		case 'Saffron'	:	$('#col1').append(this.code);
							$('#block' + idx).addClass("blSaffron");
							break;
		case 'Green'		: 	$('#col4').append(this.code);
							$('#block' + idx).addClass("blGreen");
							break;
	}
	
	$('#block' + idx).click(function() {
		selectedBlock = idx;
		$('.blSelected').removeClass('blSelected');
		$('#block' + idx).addClass('blSelected');
	});
	

	
}

Block.prototype.goHome = function() {
	switch (this.user) {
		case 'Yellow'	:	$('#block' + this.idx).appendTo($('#col2'));
							break;
		case 'Blue'		: 	$('#block' + this.idx).appendTo($('#col3'));
							break;
		case 'Saffron'	:	$('#block' + this.idx).appendTo($('#col1'));
							break;
		case 'Green'		: 	$('#block' + this.idx).appendTo($('#col4'));
							break;
	}
}

Block.prototype.goTo = function(colId) {
	myClass = 'bl' + this.user;
	if (colId<100) {
		$('#block' + this.idx).appendTo($('#col'+colId));
		return;
	}
	
	eval('isPalace=c' + parseInt(colId-100)+'.palace');
	if (isPalace != 1) {
		$('#col'+colId).children().each( function(index, domEle) {
			if (!($(domEle).hasClass(myClass))) {
				if (isTeamMode == 0) {
					objBlock = 'b' + domEle.id.substr(5);
					generateThrow();
					eval(objBlock + '.goHome();');
				} else {
					if (!($(domEle).hasClass(team[myClass]))) {
						objBlock = 'b' + domEle.id.substr(5);
						generateThrow();
						eval(objBlock + '.goHome();');
					}
				}
			}
		});	
	}
	
	$('#block' + this.idx).appendTo($('#col'+colId));
	if (colId == centralCell) {
		alert('Great!');
		$('#block' + this.idx).remove();
	}
	
}

Block.prototype.goBy = function(num) {
	user = this.user;
	colNum = $('#block' + this.idx).parent().attr("id").substr(3);
	currentColumn = this.getCurrentColumn();
	newColumnIdx = currentColumn+num;
	if (newColumnIdx >= path[user].length ) {
		alert('Cannot move further');
		return;
	}
	colTarget = path[user][newColumnIdx];
	this.goTo(colTarget);
}

Block.prototype.getCurrentColumn = function() {
	user = this.user;
	colNum = $('#block' + this.idx).parent().attr("id").substr(3);
	for (i=0; i<path[user].length; i++) {
		if (path[user][i] == colNum) {
			break;
		}
	}
	return i;
}